package vip.huhailong.rabbitmqdemo.config;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;
import vip.huhailong.rabbitmqdemo.entity.FieldContent2;

import java.io.IOException;
import java.util.List;

/**
 * @author Huhailong
 * @Description 消息监听类
 * @Date 2021/11/18.
 */
@Slf4j
@Component
public class DirectReceiver {

    @RabbitListener(queues = "TestDirectQueue")
    public void process(Message message){
        String messageId = message.getMessageProperties().getMessageId();
        Object token = message.getMessageProperties().getHeader("token");
        log.info("message id :{}", messageId);
        log.info("token :{}", token);
        List<FieldContent2> result = byte2Object(message.getBody());
        for (FieldContent2 fieldContent2 : result) {
            log.info("content:{}",fieldContent2);
        }
    }

    private List<FieldContent2> byte2Object(byte[]bytes){
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(bytes, new TypeReference<List<FieldContent2>>() {});
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


}
